package util;

public class AddressParser {
    private String protocol;
    private String host;
    private int port;
    private String obj;

    public String getProtocol() {
        return protocol;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public String getObj() {
        return obj;
    }

    public static boolean isIp(){
        return false;
    }

    public static boolean isPort(int port){
        if(port >=0 && port <= 65535){
            return true;
        }else {
            return false;
        }
    }

    public static boolean isAddress(String address){
        //Reference: https://www.cnblogs.com/gongqijundemao/p/11049785.html
        address = address.toLowerCase();
        String regex = "^((rmi|ldap)?://)"  //https、http、ftp、rtsp、mms
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@
                + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 例如：199.194.52.184
                + "|" // 允许IP和DOMAIN（域名）
                + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.
                + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名
                + "[a-z]{2,6})" // first level domain- .com or .museum
                + "(:[0-9]{1,5})?" // 端口号最大为65535,5位数
                + "((/?)" // a slash isn't required if there is no file name
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        return  address.matches(regex);
    }

    public  void parser(String address){
        //ldap://xxxx:1099/Exploit
        this.protocol = address.split(":")[0];
        String[] ad = address.split("/");
        this.host = ad[ad.length-2].split(":")[0];

        int port = Integer.valueOf(ad[ad.length-2].split(":")[1]);
        if(isPort(port)){
            this.port = port;
        }

        this.obj = ad[ad.length-1];
    }

    public static void main(String[] args) {
        System.out.println(AddressParser.isAddress("ldap://127.0.0.1:74444/sdsd"));
    }

}
